<template>
    <div v-if="store.auth.role!='学生'">
    <el-button size="small" type="success" @click="outport()">
        导出
    </el-button>
    </div>
    <el-form :inline="true" :model="brand" class="demo-form-inline" v-if="store.auth.role!='学生'">
        <el-form-item>
            <el-form-item label="绩点">
                <el-input v-model="brand.graduationGredit" placeholder="输入毕业所需绩点"></el-input>
            </el-form-item>
            <el-form-item label="英语四级">
                <el-input v-model="brand.englishTestFour" placeholder="输入毕业所需英语四级成绩"></el-input>
            </el-form-item>
            <el-form-item label="计算机等级">
                <el-input v-model="brand.computerLevel" placeholder="输入毕业所需计算机等级"></el-input>
            </el-form-item>
            <el-form-item label="学分">
                <el-input v-model="brand.totalCredit" placeholder="输入毕业所需学分"></el-input>
            </el-form-item>
            <el-button type="primary" @click="search()">查询</el-button>
            <el-upload
                    :show-file-list="false"
                    :before-upload="beforeUpload"
                    :on-success="onSuccess"
                    :on-error="onError"
                    :disabled="importDisabled"
                    style="display: inline-flex;margin-right: 10px;"
                    :action="'http://localhost:8080/data/importNewNeed?user='+store.auth.user">
                <el-button size="small" :disabled="importDisabled" type="success">
                    <el-icon class="el-icon--left"><Upload /></el-icon>{{importDataText}}
                </el-button>
                <a href="http://localhost:8080/data/download">下载模板</a>
            </el-upload>
        </el-form-item>
    </el-form>
    <el-table :data="datas.slice((pageNum-1)*pageSize,pageNum*pageSize)" style="width: 100%" max-height="250" border>
        <el-table-column fixed label="序号" width="200">
            <template #default="scopes">
                {{ scopes.$index+1 }}
            </template>
        </el-table-column>
        <el-table-column fixed prop="id" label="学号" width="200" />
        <el-table-column fixed prop="name" label="姓名" width="200" />
        <el-table-column fixed prop="college" label="学院" width="200" />
        <el-table-column prop="gredit" label="绩点" width="200" />
        <el-table-column prop="englishTestFour" label="英语四级" width="200" />
        <el-table-column prop="computerLevel" label="计算机等级" width="200" />
        <el-table-column prop="credit" label="总计学分" width="200" />
        <el-table-column prop="ispass" label="是否达到毕业标准" width="200" />
        <el-table-column prop="ispassg" label="是否达到授位标准" width="200" />
    </el-table>
    <div class="pagination">
        <el-pagination
                background
                layout="sizes,prev,pager,next,jumper,->,total"
                :page-size.sync="pageSize"
                :current-page.sync="pageNum"
                :page-sizes="[2,8,10,15]"
                :total="totals"
                :hide-on-single-page="true"
                @current-change="getList"
                @size-change="getList1"
        />
    </div>
</template>
<script setup>
import { get,post } from '@/utils/request.js'
import {ElMessage} from 'element-plus'
import router from '@/router'
import { reactive } from 'vue'
import { ref,onMounted } from 'vue'
import {userStore} from '@/stores/counter.js'
import {Upload} from '@element-plus/icons-vue'

const importDataText=ref('导入最新培养方案')
const importDisabled=ref(false)
const totals=ref('')
const pageNum=ref(1)
const pageSize=ref(2)
const store=userStore()
const datas=ref([])
const brand = reactive({
  graduationGredit:'',
  englishTestFour:'',
  computerLevel:'',
  totalCredit:''
})

if(store.auth.role == '管理员'){
get('/exam/atain',(message)=>{
datas.value.push(...message.allpass.map(item => ({ ...item, ispass: '达到',ispassg:'达到' })));
datas.value.push(...message.gpass.map(item => ({ ...item, ispass: '达到',ispassg:'未达到' })));
datas.value.push(...message.inpass.map(item => ({ ...item, ispass: '未达到',ispassg:'未达到' })));
totals.value=datas.value.length
},(message)=>{
ElMessage.warning(message)
})
}else if(store.auth.role == '辅导员'){
post('/exam/atainT',{
user:store.auth.user
},(message)=>{
console.log(message.pass)
datas.value.push(...message.allpass.map(item => ({ ...item, ispass: '达到',ispassg:'达到' })));
datas.value.push(...message.gpass.map(item => ({ ...item, ispass: '达到',ispassg:'未达到' })));
datas.value.push(...message.inpass.map(item => ({ ...item, ispass: '未达到',ispassg:'未达到' })));
totals.value=datas.value.length
},(message)=>{
ElMessage.warning(message)
})
}else if(store.auth.role == '学生'){
post('/exam/atainstu',{
user:store.auth.user
},(message)=>{
console.log(message)
if(message.allpass.length!=0) datas.value.push(...message.allpass.map(item => ({ ...item, ispass: '达到',ispassg:'达到' })));
else if(message.gpass.length!=0) datas.value.push(...message.gpass.map(item => ({ ...item, ispass: '达到',ispassg:'未达到' })));
else datas.value.push(...message.inpass.map(item => ({ ...item, ispass: '未达到',ispassg:'未达到' })));
totals.value=1
},(message)=>{
ElMessage.warning(message)
})
}

const search=()=>{
datas.value=[]
post('/exam/needexamin',{graduationGredit:brand.graduationGredit,
englishTestFour:brand.englishTestFour,
computerLevel:brand.computerLevel,
totalCredit:brand.totalCredit
},(message)=>{
console.log(message.pass)
datas.value.push(...message.pass)
  totals.value = datas.value.length
},(message)=>{
ElMessage.warning(message)
})
}

const getList=(pageNums)=>{
pageNum.value=pageNums
}

const getList1=(pageSizes)=>{
pageSize.value=pageSizes
}

const outport=()=>{
post('/exam/outport',{data:JSON.stringify(datas.value)},(message)=>{
ElMessage.success(message)
window.open('http://localhost:8080/exam/handle')
},(message)=>{
ElMessage.warning(message)
})
}

</script>
<style scoped>
</style>